package e.odbo.spring.config;


import com.openbravo.data.loader.DBSession;
import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.EntityManager;
import e.odbo.data.dao.table.I_TableSentenceBuilder;
import e.odbo.data.sample.lock.OdbOLockDAO;
import e.odbo.data.sample.tableid.TableIdGeneratorDAO;
import e.odbo.data.service.id.I_NumberIdGeneratorService;
import e.odbo.data.service.id.impl.OdbOIdGeneratorService;
import e.odbo.data.service.lock.I_LockService;
import e.odbo.data.service.lock.impl.OdbOLockService;
import e.odbo.spring.SpringDataSourceSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ConditionalOnClass({EntityManager.class})
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(OdbOProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class OdbOAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(OdbOAutoConfiguration.class);

    private final OdbOProperties properties;

    private final ApplicationContext applicationContext;


    public OdbOAutoConfiguration(OdbOProperties properties, ApplicationContext applicationContext) {
        this.properties = properties;
        this.applicationContext = applicationContext;
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringDataSourceSession getDBSession(DataSource dataSource)throws Exception{
        if(properties.getIsolationLevel()!=null)
            return new SpringDataSourceSession(dataSource,properties.dialect,properties.getIsolationLevel());
        return new SpringDataSourceSession(dataSource,properties.dialect);
    }

    @Bean
    @ConditionalOnMissingBean
    public I_TableSentenceBuilder tableSentenceBuilder(DBSession dbSession){
        return new TableDBSentenceBuilder(dbSession);
    }

    @Bean
    @ConditionalOnMissingBean
    public EntityManager entityManager(DBSession dbSession, I_TableSentenceBuilder tableSentenceBuilder){
        return new EntityManager(dbSession,tableSentenceBuilder);
    }

    @Bean(name="dbLockService")
    public I_LockService dbLockService(DBSession dbSession, I_TableSentenceBuilder tableSentenceBuilder){
        return new OdbOLockService(new OdbOLockDAO(dbSession,tableSentenceBuilder));
    }

    @Bean(name = "dbNumberIdGeneratorService")
    public I_NumberIdGeneratorService dbNumberIdGeneratorService(DBSession dbSession, I_TableSentenceBuilder tableSentenceBuilder, I_LockService lockService){
        return new OdbOIdGeneratorService(new TableIdGeneratorDAO(dbSession,tableSentenceBuilder),lockService);
    }
}
